﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceInvaders
{
    public static class TecladoVirtual
    {
        static string alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ12";
        static Texture2D tPanel;
        static Texture2D tKeyboard;
        static List<Rectangle> characters = new List<Rectangle>();
        static Vector2 posPanel;
        static Vector2 posKeyboard;

        public static void Load(/*Texture2D tPanel, Vector2 posPanel*/Texture2D tKeyboard)
        {
            //TecladoVirtual.tPanel = tPanel;
            //TecladoVirtual.posPanel = posPanel;
            TecladoVirtual.tKeyboard = tKeyboard;
            posKeyboard = new Vector2(400, 300);

            for (int y = 0; y < 3; y++)
            {
                for (int x = 0; x < 8; x++)
                {
                    if (y < 2)
                    {
                        characters.Add(new Rectangle((x * 55) + (int)posKeyboard.X, (y * 35) + (int)posKeyboard.Y, 55, 35));
                    }
                    else
                    {
                        if (x > 5)
                            break;
                        characters.Add(new Rectangle((x * 55) + (int)posKeyboard.X, (y * 35) + (int)posKeyboard.Y, 55, 35));
                    }
                }
            }
        }

        public static void Update(MouseState mState, ref string name)
        {
            if (mState.LeftButton == ButtonState.Pressed)
            {
                
                for (int n = 0; n < characters.Count; n++)
                {
                    if (characters[n].Intersects(new Rectangle(mState.X, mState.Y, 1, 1)))
                    {
                        if (alfabeto[n] == '1')
                        {
                            if (name != null || name.Length > 1)
                                name.Remove(name.Length - 1);
                        }
                        else if (alfabeto[n] == '2')
                        {
                            Ranking.WriteScore(name);

                        }
                        else
                        {
                            name = name + alfabeto[n];
                        }
                    }
                }
            }
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(tKeyboard, posKeyboard, Color.White);            
        }

    }

   
}
